package cn.david.pet.service.impl;


import cn.david.basic.service.impl.BasicServiceImpl;
import cn.david.basic.util.DistanceUtil;
import cn.david.basic.util.PageList;
import cn.david.basic.util.Point;
import cn.david.basic.util.SmsUtils;
import cn.david.org.domain.Employee;
import cn.david.org.domain.Shop;
import cn.david.org.mapper.EmployeeMapper;
import cn.david.org.mapper.ShopMapper;
import cn.david.pet.domain.Pet;
import cn.david.pet.domain.SearchMasterMsg;
import cn.david.pet.mapper.SearchMasterMsgMapper;
import cn.david.pet.query.SearchMasterMsgQuery;
import cn.david.pet.service.IPetService;
import cn.david.pet.service.ISearchMasterMsgService;
import cn.david.user.domian.User;
import cn.david.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BasicServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IPetService petService;
    public void publish(SearchMasterMsg searchMasterMsg, Long loginInfoId) {
        //寻主最近的店铺
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        //计算最近的地方
        List<Shop> shops = shopMapper.loadAll();
        Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
        //绑定最近的店铺
        searchMasterMsg.setShop_id(nearestShop.getId());
        //绑定用户信息
        User user = userMapper.loadByLogininfoId(loginInfoId);
        searchMasterMsg.setUser_id(user.getId());
        //保存寻主消息
        searchMasterMsgMapper.save(searchMasterMsg);
        //发送消息
        Employee employee = employeeMapper.loadById(nearestShop.getAdmin_id());
        //SmsUtils.send(employee.getPhone(), "老板您好，来【"+searchMasterMsg.getTitle()+"】活了，赶快去接客!");



    }

    @Override//查询未出处理的
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Long id) {
        query.setState(0);
        Employee employee = employeeMapper.loadByLoginInfoId(id);
        if(employee.getShop_id() != null){//属于店铺的消息
            query.setShopId(employee.getShop_id());
        }
        return super.queryData(query);
    }

    @Override//查询以处理的
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Long id) {
        query.setState(1);
        Employee employee = employeeMapper.loadByLoginInfoId(id);
        if(employee.getShop_id() != null){//属于店铺的消息
            query.setShopId(employee.getShop_id());
        }
        return super.queryData(query);
    }

    @Override//查询是哪个用户的
    public PageList<SearchMasterMsg> myMsg(SearchMasterMsgQuery query, Long id) {
        User user = userMapper.loadByLogininfoId(id);
        query.setUserId(user.getId());
        return super.queryData(query);
    }

    @Override
    public void handle(Pet pet) {
        //1.调整信息状态
        searchMasterMsgMapper.updateState4Proceed(pet.getSearch_master_msg_id());
        //2.保存宠物信息  返回自增id
        //3.保存宠物详情 设置宠物id
        petService.add(pet);

    }
//    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt) {
//        PetAcquisitionOrder order = new PetAcquisitionOrder();
//        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
//        order.setState(0);//待支付
//        order.setPrice(pet.getCostprice());
//        order.setAddress(adopt.getAddress());
//        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
//        order.setOrderSn(orderSn);
//        order.setPet_id(pet.getId());
//        order.setUser_id(adopt.getUser_id());
//        order.setPaytype(0);
//        order.setShop_id(pet.getShop_id());
//        order.setEmp_id(EmployeeContext.getLoginEployee().getId());
//        return order;
//    }
}
